<template>
	<el-dialog v-model="showModal" class="peak-dialog" :close-on-click-modal="false" :title="title" :width="900">
		<el-form ref="paramsFormRef" class="params-form" :inline="true" label-position="right" label-width="90px" :model="paramsForm" :rules="paramsRules">
			<el-row :gutter="16" justify="start">
				<el-col :span="12">
					<el-form-item class="form-item" label="应用名称:" prop="name">
						<el-input v-model="paramsForm.name" placeholder="请输入应用名称" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="form-item" label="所属单位:" prop="subject">
						<el-input v-model="paramsForm.subject" placeholder="请输入所属单位" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="form-item" label="IP白名单:" prop="ipWhites">
						<el-input v-model="paramsForm.ipWhites" placeholder="请输入IP白名单(多个以，分割)" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="form-item" label="回调地址:" prop="callbackUrl">
						<el-input v-model="paramsForm.callbackUrl" placeholder="请输入回调地址" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="form-item" label="应用状态:" prop="appStatus">
						<el-radio-group v-model="paramsForm.appStatus">
							<el-radio value="1">启用</el-radio>
							<el-radio value="2">停用</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="form-item" label="限制时间:" prop="accessTimeLimit">
						<el-date-picker v-model="paramsForm.accessTimeLimit" placeholder="请选择限制时间" type="date" value-format="YYYY-MM-DD" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="form-item" label="限制次数:" prop="limitLoginNum">
						<el-input-number v-model="paramsForm.limitLoginNum" class="el-form-item" controls-position="right" :min="-1" placeholder="请输入限制次数" :step-strictly="true" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="form-item" label="应用类型:" prop="appType">
						<el-select v-model="paramsForm.appType" class="el-form-item" placeholder="请选择应用类型">
							<el-option label="API接口" value="1" />
							<el-option label="统一认证" value="2" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="form-item" label="登出地址:" prop="outUrl">
						<el-input v-model="paramsForm.outUrl" placeholder="请输入登出回调地址" />
					</el-form-item>
				</el-col>

				<el-col :span="12">
					<el-form-item class="form-item" label="联系人员:" prop="linkPerson">
						<el-input v-model="paramsForm.linkPerson" placeholder="请输入联系人员" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="form-item" label="联系电话:" prop="linkPhone">
						<el-input v-model="paramsForm.linkPhone" placeholder="请输入联系电话" />
					</el-form-item>
				</el-col>

				<el-col :span="24">
					<el-form-item class="form-item" label="应用描述:" prop="appDesc">
						<el-input v-model="paramsForm.appDesc" rows="3" type="textarea" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="handleCancel">取 消</el-button>
				<el-button :loading="loading" type="primary" @click="handleSure" v-throttle>确 定</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script setup>
import global from '@/utils/global'
const emit = defineEmits(['peak-modal-ok'])
/*
 *@Description: 初始化数据
 *@Author: liuYuFeng
 *@Date: 2023-02-24 16:33:25
 */
const state = reactive({
	showModal: false,
	paramsFormRef: null,
	title: '',
	paramsForm: {
		name: '',
		subject: '',
		appStatus: '1',
		limitLoginNum: -1,
		accessTimeLimit: null,
		ipWhites: '',
		callbackUrl: '',
		outUrl: '',
		appType: '1',
		linkPerson: '',
		linkPhone: '',
		appDesc: '',
	},
	paramsRules: {
		name: [{ required: true, message: '请输入应用名称', trigger: 'blur' }],
		subject: [{ required: true, message: '请输入所属单位', trigger: 'blur' }],
	},
	loading: false,
})
const { showModal, loading, title, paramsForm, paramsRules, paramsFormRef } = toRefs(state)

/*
 *@Description: 方法method
 *@Author: liuYuFeng
 *@Date: 2023-02-24 15:34:16
 */
const handleSure = () => {
	state.paramsFormRef.validate(valid => {
		if (valid) {
			handleSureOk()
		} else {
			return false
		}
	})
}
const handleSureOk = () => {
	state.loading = true
	if (state.paramsForm.id) {
		//编辑
		global.$fetchPostData(`/meta-system/web/v1/authApplication/edit`, state.paramsForm).then(data => {
			state.loading = false
			if (data.code == 200) {
				state.showModal = false
				emit('peak-modal-ok', 2)
			} else {
				global.$baseMessage(data.msg, 'error')
			}
		})
	} else {
		global.$fetchPostData(`/meta-system/web/v1/authApplication/add`, state.paramsForm).then(data => {
			state.loading = false
			if (data.code == 200) {
				state.showModal = false
				emit('peak-modal-ok', 1)
			} else {
				global.$baseMessage(data.msg, 'error')
			}
		})
	}
}
const handleCancel = () => {
	state.showModal = false
}
const handleShowModal = async row => {
	state.showModal = true
	await nextTick()
	state.paramsForm.id = ''
	if (row) {
		state.title = '编辑应用'
		state.paramsFormRef.resetFields()
		getDataDetail(row.id)
	} else {
		state.title = '新增应用'
		state.paramsFormRef.resetFields()
	}
}
const getDataDetail = id => {
	global.$fetchGetData(`/meta-system/web/v1/authApplication/details`, { id: id }).then(data => {
		if (data.code == 200) {
			state.paramsForm = data.data || {}
		} else {
			global.$baseMessage(data.msg, 'error')
		}
	})
}
defineExpose({
	handleShowModal,
})
</script>
<style lang="scss" scoped></style>
